<?php
class EmpresasController extends AppController {

	var $name = 'Empresas';
    var $paginate = "";
	function index() {
		$this->Empresa->recursive = 0;
		$this->set('empresas', $this->paginate());
	}
    
    function consultadetalle($did = null)
    {
        $actualizaciones = $this->Empresa->query("select * from detalles_actualizaciones where id like $did");
        $this->set(compact("actualizaciones"));   
    }
    
    function actualizaciones($eid = null)
    {
        $actualizaciones = $this->Empresa->Actualizacion->find('all', array("conditions"=>array("Empresa.id" => $eid)));

        $this->set(compact("actualizaciones"));    
    }
    
    function actualizacion($aid = null)
    {
              $actualizaciones = $this->Empresa->Actualizacion->find('all', array("conditions"=>array("Actualizacion.id" => $aid)));

        $this->set(compact("actualizaciones"));  
    }

    function areas($empresa_id)                         
    {
        $ajaxx = 0;
        $headers = getallheaders();
        if (!empty($this->data)) {
            $this->Empresa->Area->create();
            if ($this->Empresa->Area->save($this->data)) {
            } else {
                $this->Session->setFlash(__('The area could not be saved. Please, try again.', true));
            }
            

        }
            
        if (isset($headers['x-requested-with']))
        {
            $this->layout = 'ajax';
            $ajaxx = 1;
        }
        
        $this->set(                             
                'areas', $this->paginate(
                    $this->Empresa->Area, array("Area.empresa_id" => $empresa_id)
                 )
            );
        $this->set('empresa', $this->Empresa->read(null, $empresa_id));  
        $this->set('usuarios', $this->Empresa->Usuario->find('list'));
        $this->set(compact('ajaxx'));
    }
    
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid empresa', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('empresa', $this->Empresa->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Empresa->create();
			if ($this->Empresa->save($this->data)) {
				$this->Session->setFlash(__('The empresa has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The empresa could not be saved. Please, try again.', true));
			}
		}
		$usuarios = $this->Empresa->Usuario->find('list');
		$areas = $this->Empresa->Area->find('list');
		$this->set(compact('usuarios', 'areas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid empresa', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Empresa->save($this->data)) {
				$this->Session->setFlash(__('The empresa has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The empresa could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Empresa->read(null, $id);
		}
		$usuarios = $this->Empresa->Usuario->find('list');
		$areas = $this->Empresa->Area->find('list');
		$this->set(compact('usuarios', 'areas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for empresa', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Empresa->delete($id)) {
			$this->Session->setFlash(__('Empresa deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Empresa was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
